package course

import (
	"aifuture.cloud/response/course"
	"encoding/json"
	"fmt"
)

type CourSvc struct {
	queryInteraptor QuertInter //查询处理器
}

func NewCourSvc(q QuertInter) *CourSvc {
	return &CourSvc{queryInteraptor: q}
}
func (c *CourSvc) List() (error, course.CourseLists) {
	//查询数据库返回信息列表
	data := c.queryInteraptor.List()
	list := course.CourseLists{
		Data:      data, //mock数据从数据库中查询出来即可
		Page:      1,
		PageSize:  3,
		TotalSize: len(data),
	}
	return nil, list
}

type CourseDetailInfo struct {
	CourseId           int                 `json:"course_id"`
	Image              string              `json:"image"`
	TearcherImage        string              `json:"tearcher_image"`
	Title              string              `json:"title"`
	Desc               string              `json:"desc"`
	YouhuiEvent        Event               `json:"youhui_event"`
	CourseJiangzuoInfo CourseJiangzuoInfo  `json:"course_jiangzuo_info"`
	CourseOutline      interface{}         `json:"course_outline"`
	Detail             CourseDetailInfoNei `json:"detail"`
}
type CourseJiangzuoInfo struct {
	JiangzuoId   int    `json:"jiangzuo_id"`
	Tearcher     string `json:"tearcher"`
	CatetoryId   int    `json:"catetory_id"`
	CatetoryName string `json:"catetory_name"`
}

type CourseDetailInfoNei struct {
	Tearcher     string   `json:"tearcher"`
	Tel          string   `json:"tel"`
	QrCode       string   `json:"qr_code"`
	CharpterInfo []string `json:"charpterInfo"`
	Markdowndata string   `json:"markdowndata"`
}

type Event struct {
	IsFree bool    `json:"is_free"`
	Aprice float64 `json:"aprice"`
	Yprice float64 `json:"yprice"`
}
type CourseOutline struct {
	Chapter     int            `json:"chapter"`
	Title       string         `json:"title"`
	ChapterList []*ChapterList `json:"chapter_list"`
}
type ChapterList struct {
	Chapter      int    `json:"chapter"`
	SerialNumber string `json:"serial_number"`
	Title        string `json:"title"`
	IsFree       bool   `json:"is_free"`
	TimeDur      string `json:"time_dur"`
}

func (c *CourSvc) MockData(id int) (error, interface{}) {
	var data interface{}
	course_id := id
	fmt.Println(course_id)
	mock_data := `[{"key":"grps","icon":null,"children":[{"key":"13","label":"第1讲 (免费) 软件测试行业发展前景1 55:59"},{"key":"14","label":"第1讲 (免费) 软件测试行业发展前景1 55:59"}],"label":"第一章","type":"group"},{"key":"grp","icon":null,"children":[{"key":"15","label":"第1讲 (免费) 软件测试行业发展前景1 55:59"},{"key":"16","label":"第1讲 (免费) 软件测试行业发展前景1 55:59"}],"label":"第二章","type":"group"}]`
	err := json.Unmarshal([]byte(mock_data), &data)
	if err != nil {
		fmt.Println(err.Error())
	}
	return nil, data
}

func (c *CourSvc) detail(id int) *course.CourseResponse {
	course1 := &course.CourseResponse{
		CourseId:     1,
		Title:        "试听课-Python入门课",
		Price:        9.9,
		YPrice:       0,
		LearnPeCount: 12269,
		IsLearning:   0,
		IsLogin:      0,
		Image:        "https://www.lanboai.cn/gateway/system/images/e7c911bbb05940a6bd57a50906689cf3.png",
		LearnUrl:     "",
	}
	course2 := &course.CourseResponse{
		CourseId:     2,
		Title:        "软件测试工程师",
		Price:        10600,
		YPrice:       8600,
		LearnPeCount: 4784,
		IsLearning:   0,
		IsLogin:      0,
		Image:        "https://www.lanboai.cn/gateway/system/images/235fcf4eb5484ec589722d2f15acb70d.png",
	}

	course3 := &course.CourseResponse{
		CourseId:     3,
		Title:        "人工智能高级算法工程师",
		Price:        36800,
		YPrice:       32800,
		LearnPeCount: 20654,
		IsLearning:   0,
		IsLogin:      0,
		Image:        "https://www.lanboai.cn/gateway/system/images/2fa3d1a5470e4c5e82db258d5e2b6674.png",
	}

	course4 := &course.CourseResponse{
		CourseId:     4,
		Title:        "国内外人工智能科创竞赛【咨询】",
		Price:        99,
		YPrice:       9.9,
		LearnPeCount: 29000,
		IsLearning:   0,
		IsLogin:      0,
		Image:        "https://www.lanboai.cn/gateway/system/images/8b415c72d9f34b77895ac38480d25196.png",
	}
	data := make(map[int]*course.CourseResponse, 0)
	data[1] = course1
	data[2] = course2
	data[3] = course3
	data[4] = course4
	return data[id]

}

func (c *CourSvc) Detail(id int) (error, interface{}) {
	_, CourseOutline := c.MockData(id)
	//
	course_id := id
	detailInfo := c.detail(id)
	isFree := true
	currentPrice := detailInfo.YPrice
	if currentPrice != float64(0) {
		isFree = false
	}
	//
	courseInfo := &CourseDetailInfo{
		CourseId: course_id,
		Image:    detailInfo.Image,
		TearcherImage: "https://www.lanboai.cn/gateway/system/images/7cbe0cc35e6e40ec9a43eafc42cd51ac.jpg",
		Title:    detailInfo.Title,
		Desc:     "（含课件+代码实操）",
		YouhuiEvent: Event{
			IsFree: isFree,
			Aprice: detailInfo.YPrice,
			Yprice: detailInfo.Price,
		},
		CourseJiangzuoInfo: CourseJiangzuoInfo{
			JiangzuoId:   1,
			Tearcher:     "深兰教育",
			CatetoryId:   1,
			CatetoryName: "热门课程",
		},
		Detail: CourseDetailInfoNei{
			Tearcher: "王老师",
			Tel:      "17336292096",
			QrCode:   "https://www.lanboai.cn/gateway/system/images/6d19f9b833364d0db0b9e2122c6e4726.jpg",
			CharpterInfo: []string{
				"第1章 环境安装", "第2章 python基础",
				"第3章 列表元组字典集合", "第4章 条件控制循环语句",
				"第5章 函数", "第6章 面向对象",
				"第7章 模块和包", "第8章 文件和异常", "第9章 正则表达式", "第10章 python高级编程",
				"第11章 Numpy", "第12章 pandas", "第13章 matplotlib",
			},
			Markdowndata: `# 购买前请咨询工作人员详情：深兰教育王老师 17336292096
 ![这是图片]( https://www.lanboai.cn/gateway/system/images/3478421087324537b3737e22926a6434.jpg)`,
		},
		CourseOutline: CourseOutline,
	}

	return nil, courseInfo
}
